package kr.saeildamil.androidforstudy2.ch28;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainXml extends Activity {
	ProgressDialog mProgress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		Button pullBtn = new Button(this);
		pullBtn.setText("PullParser");
		pullBtn.setOnClickListener(listener);
		pullBtn.setId(1);
		
		Button jsonBtn = new Button(this);
		jsonBtn.setText("JsonParser");
		jsonBtn.setOnClickListener(listener);
		jsonBtn.setId(2);
		
		LayoutParams params = new LayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,(LayoutParams.WRAP_CONTENT)));
		
		layout.addView(pullBtn, params);
		layout.addView(jsonBtn, params);
		
		setContentView(layout);
		
		new XmlJsonMake().execute();
	}
	
	private class XmlJsonMake extends AsyncTask<Void, Void, Void>{
		FileOutputStream fos = null;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgress = ProgressDialog.show(MainXml.this, "Wait", "Now Making order.xml & order.json");
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			try {
				fos = openFileOutput("order.xml", MODE_PRIVATE);
				String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" 
						+ " <order>\n" 
						+ "      <item Maker=\"Samsung\" Price=\"23000\">Mouse</item>\n"
						+ "      <item Maker=\"LG\" Price=\"12000\">KeyBoard</item>\n"
						+ "      <item Price=\"156000\" Maker=\"WesternDigital\" >HDD</item>\n"
						+ "</order>";
				fos.write(xml.getBytes());
				fos = openFileOutput("order.json", MODE_PRIVATE);
				String json = "[\n"
						+ "{\"Product\":\"Mouse\", \"Maker\":\"Samsung\", \"Price\":23000},\n"
						+ "{\"Product\":\"KsyBoard\", \"Maker\":\"LG\", \"Price\":12000},\n"
						+ "{\"Product\":\"HDD\", \"Maker\":\"WesternDigital\", \"Price\":156000},\n"
						+ "]";
				fos.write(json.getBytes());
				fos.close();
				Thread.sleep(3000);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}finally{
				try {
					fos.close();
				} catch (IOException e) {}
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			mProgress.dismiss();
			Toast.makeText(MainXml.this, "Completed.", Toast.LENGTH_SHORT).show();
		}
		
	}
	
	View.OnClickListener listener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			switch (v.getId()) {
			case 1:
				intent.setClass(MainXml.this, PullParserActivity.class);
				break;
			case 2:
				intent.setClass(MainXml.this, JsonParserActivity.class);
				break;
			}
			startActivity(intent);
		}
	};
	
}
